package service;

import com.sun.xml.internal.bind.v2.model.core.ID;
import lombok.extern.slf4j.Slf4j;

import javax.sound.midi.Soundbank;
import javax.swing.*;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @author: hett
 * @date: 2022/3/11 17:30
 */
public class BIOService {

    private static int port = 3210;

    private static Socket socket;

    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(port);
            BufferedInputStream bis = null;
            OutputStream os = null;
            byte[] bytes = new byte[1];
            while (true) {
                System.out.println("开启监听");
                socket = serverSocket.accept();
                bis = new BufferedInputStream(socket.getInputStream());
                os = socket.getOutputStream();
                StringBuilder message = new StringBuilder();
                while ((bis.read(bytes)) != -1) {
                    String data = new String(bytes);
                    System.out.println(data);
                    message.append(new String(bytes));
                    if (bis.available() == 0){
                        System.out.println(message);
                        message.delete(0,message.length());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
